<?php
/**
 * Created by PhpStorm.
 * User: huawei
 * Date: 2021/11/1
 * Time: 17:19
 */

namespace app\index\home;

use think\Exception;
use yuexiu\Client;
use yuexiu\Wx;
use think\Db;

class Main extends Common
{
    public function test(){
        $client = new Client();
        $client->couponlist();
//        $client->getredbaglist();
//        $client->sendredbag(1,USERID);
    }
    public function userinfo(){
        $item = Db::name("userinfo")->where("id",USER)->field("nickname,avatarUrl")->find();
        okMsg($item);
    }
    /**
     * @title 获取二维码
     */
    public function getqrcode(){
        $typec = $this->request->get("typec"); //二维码类型 1样式1，2样式2，3样式3
        if(!in_array($typec,[1,2,3])){
            errMsg("参数错误");
        }
        $user = Db::name("userinfo")->where("id",USER)->value("visitorStatus");
        $uarra = explode(',',$user);
        if(in_array(2,$uarra)){
            $visitorStatus = 1;
        }else{
            $visitorStatus = 0;
        }
        $client = new Client();
        $qrcode = $client->createqrcode(USER,$typec,USERID,$visitorStatus);
        okMsg(["qrcode"=>$qrcode]);
    }

    /**
     * @title 获取需要助力的用户信息
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function gethelpuser(){
        $helpid = $this->request->get("helpid");
        $user = Db::name("userinfo")->where('id',$helpid)->field("nickname,avatarUrl")->find();
        okMsg($user);
    }
    public function boost(){
        $helpid = $this->request->post("helpid");
        if($helpid==USER){
            okMsg(0,"不能给自己助力");
        }
        $haing = Db::name("boost")->where("user_id",USER)->value("id");

        if($haing){
            okMsg(1,"您已经助力过一次啦");
        }
        $today = date("Y-m-d");
//        //今日助力
//        $num = Db::name("boost")->where("to_user",$helpid)->where("add_time",'>=',$today)->count("id");
//        if($num>=2){
//            okMsg(2,"TA今日达到最大助力限制了");
//        }
        $user = Db::name("userinfo")->where('id',$helpid)->field("id,nickname,avatarUrl,game_num,boost_num,publicOpenId")->find();
        if(!$user){
            errMsg("用户不存在");
        }
        $num = $user["boost_num"];
        $num +=1;
        $uupdate = [
            "boost_num"=>$num
        ];
        if($num%2==0){
            $uupdate["game_num"] = $user["game_num"]+1;
            //两次以上了，去查找用户的公众号openid

        }
        try{
            Db::name("userinfo")->where('id',$helpid)->update($uupdate);
            Db::name("boost")->insert([
                "user_id"=>USER,
                "to_user"=>$user["id"],
                "add_time"=>date("Y-m-d H:i:s")
            ]);
            if($num%2==0) {
                $wx = new Wx();
                if ($user["publicOpenId"]) {
                    //获取当前用户昵称
                    $nickname = NICKNAME;
                    $game = intval($num/2);
                    $wx->sendmsg($user["publicOpenId"], "【{$nickname}】来助力啦~已有【{$num}】位好友为您点赞，共获得【{$game}】次游戏机会",AREA,$user["nickname"]);
                }
            }
            okMsg(3,"助力成功");
        }catch (Exception $e){
            errMsg($e->getMessage());
        }
    }

    /**
     * @title 给我助力的用户
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function boostmy(){
        $lists = Db::name("boost")
            ->field("u.nickname,u.avatarUrl")
            ->alias("b")
            ->leftJoin("yuexiu_userinfo u",'b.user_id=u.id')
            ->where("b.to_user",USER)->limit(5)->order("b.id desc")
            ->select();
        okMsg($lists);
    }

    /**
     * @title 助力数据
     */
    public function boostinfo(){
        $count = Db::name("userinfo")->where("id",USER)->value("boost_num");
        if($count<=0){
            okMsg([
                "count"=>0,
                "num"=>0
            ]);
        }else{
           $num = intval($count/2);
           okMsg([
                "count"=>$count,
                "num"=>$num
           ]);
        }
    }
}